﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/admin/user"
 
@namespace ThingsGateway.Admin.Blazor
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using System.Reflection;
@using ThingsGateway.Admin.Application;
@attribute [Authorize]
@inherits BaseComponentBase
@layout MainLayout
@inject UserResoures UserResoures
<AppDataTable @ref="_datatable" TItem="SysUser" SearchItem="UserPageInput" 
              AddItem="UserAddInput" EditItem="UserEditInput" SearchModel="@_search"
              QueryCallAsync="QueryCallAsync" AddCallAsync="AddCallAsync" EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
                IsShowQueryButton IsShowDetailButton IsShowSearchKey
              IsShowAddButton=@UserResoures.IsHasButtonWithRole("sysuseradd")
              IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("sysuserdelete")
              IsShowEditButton=@UserResoures.IsHasButtonWithRole("sysuseredit")>
    <AddTemplate>
        @GetRenderFragment(context)
    </AddTemplate>

    <EditTemplate>
        @GetRenderFragment(context)
    </EditTemplate>

    <ItemColOperTemplate>
        <MList Dense>
            @if (UserResoures.IsHasButtonWithRole("sysuserresetpassword"))
            {
                <MListItem OnClick="async()=>await ResetPasswordAsync(context.Item)">
                    <MListItemTitle Class="ml-2">重置密码</MListItemTitle>
                </MListItem>
            }

            @if (@UserResoures.IsHasButtonWithRole("sysuserperrole"))
            {
                <MListItem OnClick="async()=>
                    {
                       _choiceUserId=context.Item.Id;
                        await RoleInitAsync();
                        _isShowRoles=true;
                    }">
                    <MListItemTitle Class="ml-2">角色授权</MListItemTitle>
                </MListItem>
            }

        </MList>
    </ItemColOperTemplate>


    <ItemColTemplate>
        @switch (context.Header.Value)
        {
            case nameof(context.Item.UserEnable):
                <MSimpleCheckbox ValueChanged=@((a)=> UserStatusChangeAsync(context.Item,a))
                              Value="context.Item.UserEnable">
                </MSimpleCheckbox>
                break;
            default:
                @if (context.Header.CellClass?.Contains("text-truncate") == true)
                {
                    <span title=@context.Value>
                        @context.Value
                    </span>
                }
                else
                {
                    @context.Value
                }
                break;
        }

    </ItemColTemplate>
</AppDataTable>

<PDrawer @bind-Value="_isShowRoles" OnCancel="() => _isShowRoles = false"
         Title=角色授权
         Width=@(IsMobile?"100%":"500")
         MaxWidth="500" OnSave="OnRolesSaveAsync">
    @if (_isShowRoles)
    {
        <MCard Flat Class="ma-0 pa-4">
            <MCardTitle Class="py-2">
                <MTextField Clearable Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 ml-3" @bind-Value="_searchName"
                            Outlined Label=@typeof(SysRole).GetDescription(nameof(SysRole.Name)) />
            </MCardTitle>
            <MTreeview Class="my-1" Dense OpenAll TItem="SysRole" TKey="SysRole" Selectable
             @bind-Value="_rolesChoice" Search="@_searchName" ItemKey=@(r=>r)
                        Items="_allRoles" ItemText="r=>r.Name" ItemChildren="r=>null">
             <LabelContent>
                 <span title=@context.Item.Name>
                     @context.Item.Name
                 </span>
             </LabelContent>
             </MTreeview>
            </MCard>
    }
</PDrawer>

@code {

    RenderFragment GetRenderFragment(UserAddInput context)
    {
        RenderFragment renderFragment =
    @<div>
        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Account))  </MSubheader>
        <MTextField Disabled=@(context.Account == RoleConst.SuperAdmin) Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Account />

        <MSubheader Class="mt-4 font-weight-black">   @(context.Description(x => x.Phone))</MSubheader>
            <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Phone />

        <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Email))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Email />

        <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />

    </div>

    ;
        return renderFragment;
    }

}

